package com.test.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.test.utils.JdbcUtils;
import com.test.utils.Msg;

import net.sf.json.JSONObject;

/**
 * Servlet implementation class DianzanServlet
 * 用户点赞
 */
public class DianzanServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DianzanServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        Date date = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = sf.format(date);
        String userId = request.getParameter("userId");           //用户id
        String jingdianId = request.getParameter("jingdianId");         //景点id
       
        JdbcUtils jdbcUtils = new JdbcUtils();
        try {
			Connection conn = jdbcUtils.getConnection();
			Statement stmt = conn.createStatement();
			String sql = "INSERT INTO dianzan (user_id, jingdan_id,creattime) VALUES ('"+userId+"', '"+jingdianId+"','"+time+"')";
			int dept =  stmt.executeUpdate(sql); //执行sql语句
			System.out.println(dept);
			if(dept==1) {
				out.println(JSONObject.fromObject(Msg.success()));
			}else {
				out.println(JSONObject.fromObject(Msg.fail()));
			}
		} catch (ClassNotFoundException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
